﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Excel = Microsoft.Office.Interop.Excel;
using System.IO;

namespace QuickExcel
{
    public partial class applyBookmarks : Form
    {
        public applyBookmarks()
        {
            InitializeComponent();
        }

        private void applyBookmarks_Load(object sender, EventArgs e)
        {
            listBookmarks.Items.Clear();

            TextReader tw = new StreamReader("c:\\Program Files\\QuickExcel\\test.txt");
            String line;
            while ((line = tw.ReadLine()) != null)
            {
                String[] parts = line.Split('-');
                listBookmarks.Items.Add(parts[0]);
            }
        }

        private void cmdClose_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void cmdActivate_Click(object sender, EventArgs e)
        {
            TextReader tw = new StreamReader("c:\\Program Files\\QuickExcel\\test.txt");
            string line;
            string file_path="";
            string sheetName = "";
            string range = "";

            while ((line = tw.ReadLine()) != null)
            {
                String[] parts = line.Split('-');
                if (listBookmarks.SelectedItem.ToString() == parts[0])
                {
                    file_path = parts[1] + "\\" + parts[2];
                    
                    if (parts.Count() > 3 && parts[3] != "")
                    {
                        sheetName = parts[3];
                        if (parts.Count() > 4 && parts[4] != "")
                            range = parts[4];
                    }
                    break;
                }
            }
                Globals.ThisAddIn.Application.Workbooks.Open(file_path);
                if (sheetName != "")
                    Globals.ThisAddIn.Application.Sheets[sheetName].Activate();

                if (range != "")
                {
                    Excel.Range rng = Globals.ThisAddIn.Application.ActiveSheet.Range(range);
                    rng.Select();
                }
            this.Close();
        }
    }
}
